package com.situ.mall.common.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.situ.mall.common.entity.Cart;
import com.situ.mall.common.vo.CartVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author JavaWizard
 * @since 2023-09-19
 */
@Mapper
public interface CartMapper extends BaseMapper<Cart> {

    //根据会员id和商品id查询购物车
    @Select("select * from cart where member_id = #{memberId} and product_id = #{pId}")
    Cart selectBy2Id(@Param("memberId")int memberId, @Param("pId")Integer pId);

    //没查到，添加商品
    @Insert("insert into cart (product_id, amount, create_time, member_id) values (#{productId}, #{amount}, #{createTime}, #{memberId});")
    void insertCart(Cart newCart);

    //查到了，更新商品数量
    @Update("update cart set amount = #{amount} where id = #{id};")
    void updateCart(@Param("id")int id, @Param("amount")int amount);

    //查该会员购物车中的所有商品
    @Select("select product.*,cart.amount\n" +
            "from cart inner join product on product.id = cart.product_id\n" +
            "where member_id = #{memberId}")
    List<CartVO> selectAll(Integer memberId);

    //查购物车ids
    List<String> selectByIds(@Param("ids")String[] split, @Param("memberId")Integer memberId);

    //购物车内删除商品
    @Delete("delete from cart where product_id = #{productId} and member_id = #{memberId}")
    void deleteThis(@Param("productId")Integer productId, @Param("memberId")Integer memberId);
}
